package com.cwd.arm.application.service.write;

import com.cwd.arm.application.dto.importExccel.ProjectImportDto;
import com.cwd.arm.application.dto.importExccel.ReceiveNodeImportDto;
import com.cwd.arm.application.dto.importExccel.ReceiveRecordImportDto;
import com.cwd.arm.application.dto.importExccel.TaxImportDto;
import com.cwd.arm.application.dto.project.ProjectAddDto;
import com.cwd.arm.application.dto.receiveNode.ReceiveNodeAddDto;
import com.cwd.arm.application.dto.receiveRecord.ReceiveRecordAddDto;
import com.cwd.arm.application.dto.tax.TaxAddDto;
import com.cwd.arm.application.service.write.facade.*;
import com.cwd.arm.common.enums.ErrorEnum;
import com.cwd.arm.common.exception.ArmException;
import com.cwd.arm.infrastructure.dao.ProjectDao;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by yan on 22-4-17.
 */
@Repository
@Primary
public class ImportServiceImpl implements ImportService {

    @Autowired
    private ProjectDao projectDao;

    @Autowired
    private ProjectWriteService projectWriteService;

    @Autowired
    private ReceiveNodeWriteService receiveNodeWriteService;

    @Autowired
    private TaxWriteService taxWriteService;

    @Autowired
    private ReceiveRecordWriteService receiveRecordWriteService;

    @Transactional
    @Override
    public void importProject(List<ProjectImportDto> projectImportDtoList) throws Exception{
        for (ProjectImportDto projectImportDto : projectImportDtoList) {
            Long count = projectDao.getCount(projectImportDto.getProjectName());
            if(count != 0){
                throw new ArmException(ErrorEnum.IMPORT_ERROR,"项目'"+projectImportDto.getProjectName()+"'已存在无法导入");
            }
            Long projectId = projectWriteService.save(convertToAddDto(projectImportDto));
            List<ReceiveNodeImportDto> receiveNodeImportDtoList = projectImportDto.getReceiveNodeImportDtoList();
            for (ReceiveNodeImportDto receiveNodeImportDto : receiveNodeImportDtoList) {
                Long nodeId = receiveNodeWriteService.saveReId(convertToAddDto(receiveNodeImportDto, projectId));
                List<TaxImportDto> taxImportDtoList = receiveNodeImportDto.getTaxImportDtoList();
                for (TaxImportDto taxImportDto : taxImportDtoList) {
                    Long taxId = taxWriteService.save(convertToAddDto(taxImportDto, nodeId));
                    List<ReceiveRecordImportDto> receiveRecordImportDtoList = taxImportDto.getReceiveRecordImportDtoList();
                    for (ReceiveRecordImportDto receiveRecordImportDto : receiveRecordImportDtoList) {
                        receiveRecordWriteService.save(convertToAddDto(receiveRecordImportDto,taxId));
                    }
                }
            }
        }
    }

    private ProjectAddDto convertToAddDto(ProjectImportDto importDto) {
        ProjectAddDto addDto = new ProjectAddDto();
        BeanUtils.copyProperties(importDto, addDto);
        return addDto;
    }

    private ReceiveNodeAddDto convertToAddDto(ReceiveNodeImportDto importDto, Long projectId) {
        ReceiveNodeAddDto addDto = new ReceiveNodeAddDto();
        BeanUtils.copyProperties(importDto, addDto);
        addDto.setProjectId(projectId);
        return addDto;
    }

    private TaxAddDto convertToAddDto(TaxImportDto importDto, Long nodeId) {
        TaxAddDto addDto = new TaxAddDto();
        BeanUtils.copyProperties(importDto, addDto);
        addDto.setReceiveNodeId(nodeId);
        return addDto;
    }

    private ReceiveRecordAddDto convertToAddDto(ReceiveRecordImportDto importDto, Long taxId) {
        ReceiveRecordAddDto addDto = new ReceiveRecordAddDto();
        BeanUtils.copyProperties(importDto, addDto);
        addDto.setTaxId(taxId);
        return addDto;
    }
}
